﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using NLBS.Entity;

namespace NLBS.Data.Business
{
    public abstract class BasePageHelp<T> : IPageCache<T>
    {
       

        #region IPageCache Members
        

        private IList<int> _keyList;
        public IList<int> KeyList
        {
            get
            {
                return _keyList;
            }
            set
            {
                _keyList=value;
            }
        }

        private IPagedCollection<T> _pageList;
        public IPagedCollection<T> PageList
        {
            get
            {
                return _pageList;
            }
            set
            {
                _pageList = value;
            }
        }

        private PageSetting _setting;
        public PageSetting Setting
        {
            get
            {
                return _setting;
            }
            set
            {
                
                _setting = value;
                _setting.Start -= 1;
                _setting.End = _setting.Start + _setting.End - 1;
            }
        }

        private string _cacheKey;
        public virtual string CacheKey
        {
            get
            {
                return _cacheKey;
            }
            set
            {
                _cacheKey=value;
            }
        }

        public ContentCache cache
        {
            get
            {
                return ContentCache.Instantiate();
            }
        }

        public void CheckSetting()
        {
          
            if (Setting.End >= this.KeyList.Count)
            {
                Setting.End = this.KeyList.Count - 1;
            }
        }



        public bool CheckKeyList()
        {
            for (int i = Setting.Start; i < Setting.End; i++)
            {
                if (this.KeyList[i] == 0)
                {
                    return false;
                }
            }
            return true;
        }

       

        public virtual IPagedCollection<T> GetPageFromCatche()
        {
            this.KeyList = (List<int>)cache[CacheKey];
            IPagedCollection<T> lists = new PagedCollection<T>();
            
            if (this.KeyList != null)
            {
              
                CheckSetting();
                lists.MaxItems = this.KeyList.Count;
                if (CheckKeyList())
                {
                    for (int i = Setting.Start; i <= Setting.End; i++)
                    {
                        T art = (T)cache[string.Format(Setting.Replicekey, this.KeyList[i])];
                        if (art != null)
                        {
                            lists.Add(art);
                        }
                        else
                        {
                            lists.Clear();
                            return lists;
                        }
                    }
                    
                }
            }
            return lists;
        }

        #endregion

       

        public void SetCache(IPagedCollection<T> list)
        {
            PageList = list;
            InitListLength();
            AddCacheKey();
            AddCacheEntity();
        }

        public void InitListLength()
        {
            //if (KeyList != null)
            //{
            //    KeyList.Clear();
            //}
            if (KeyList == null ||KeyList.Count==0)
            {
                KeyList = new List<int>();
                for (int i = 0; i < PageList.MaxItems; i++)
                {
                    KeyList.Add(0);
                }
                cache.Insert(CacheKey, KeyList, CacheDuration.Long);
            }
        }
      

        public abstract void AddCacheKey();


        public abstract void AddCacheEntity();

       
    }
}
